/*
Name: Kabir Kang
Date: 7/27/14
Summary: Randomized test for embargo card
*/

#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"
#include "math.h"

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <time.h>

// set NOISY_TEST to 0 to remove printfs from output
#define NOISY_TEST 1

int const NUM_TESTS = 1000;

int embargoTest(int currentPlayer, int choice1, int choice2, int choice3, struct gameState *post, int randomSeed) {
	struct gameState preGame;
	int handPos = floor(Random() * post->handCount[currentPlayer]);
	int saveHandCount = post->handCount[currentPlayer];
	int saveCoins = post->coins;
	int saveEmbargoTokens = post->embargoTokens[choice1];
	int r;

	memcpy(&preGame, post, sizeof(struct gameState));

	SelectStream(2);
	PutSeed(randomSeed);

	r = cardEffect(embargo, choice1, 0, 0, post, handPos, 0);
	PutSeed(randomSeed);

	//Gain two coins
	preGame.coins = preGame.coins + 2;

	if (preGame.supplyCount[choice1] != -1) {
		//add embargo
		preGame.embargoTokens[choice1]++;

		discardCard(handPos, currentPlayer, &preGame, 1);
	}

#if (NOISY_TEST==1)
	printf("Checking values... \n");
#endif
	if (r == 0) 
	{
		assert(r == 0);
		assert(post->handCount[currentPlayer] == saveHandCount - 1);
		assert(post->coins == saveCoins + 2);
		assert(post->embargoTokens[choice1] == saveEmbargoTokens + 1);
		assert(memcmp(&preGame, post, sizeof(struct gameState)) == 0);
	}
	else {
		assert(r == -1);
		assert(post->coins == saveCoins + 2);
	}


	return 0;
}

int main() {
	int i, j, p, choice1;
	struct gameState g;

	srand(time(NULL));
	int randomSeed = rand();
	SelectStream(3);
	PutSeed(randomSeed);

#if (NOISY_TEST==1)
	printf("Testing embargo... \n");
	printf("Initializing games for each test... \n");
#endif

	for (i = 0; i < NUM_TESTS; i++) {
		SelectStream(3);
		for (j = 0; j < sizeof(struct gameState); j++) {
			((char*)&g)[j] = floor(Random() * 256);
		}

		p = floor(Random() * MAX_PLAYERS);
		g.whoseTurn = p;
		g.deckCount[p] = floor(Random() * MAX_DECK);
		g.discardCount[p] = floor(Random() * MAX_DECK);
		g.handCount[p] = floor(Random() * MAX_HAND);
		g.playedCardCount = floor(Random() * MAX_DECK);
		choice1 = floor(Random() * (treasure_map + 1));
		g.coins = 0;
		g.embargoTokens[choice1] = 0;
		g.supplyCount[choice1] = floor(Random() * 60) - 1;

		embargoTest(p, choice1, 0, 0, &g, randomSeed);
	}

#if (NOISY_TEST==1)
	printf("Embargo: PASS \n");
#endif
	return 0;
}